﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessEntity;

namespace BusinessService
{
    /// <summary>
    /// 维修状态
    /// </summary>
    public class MaintainService
    {
        #region 添加维修状态
        /// <summary>
        /// 添加维修状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddNew(MaintainStatus model)
        {
            object obj = SqlHelper.ExecuteScalar(
                "INSERT INTO MaintainStatus(StatusId,StatusDesc) VALUES (@StatusId,@StatusDesc);SELECT @@identity"
                , new SqlParameter("@StatusId", model.StatusId)
                , new SqlParameter("@StatusDesc", model.StatusDesc)
            );
            return Convert.ToInt32(obj);
        }
        #endregion
        #region 删除维修状态
        /// <summary>
        /// 删除维修状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            int rows = SqlHelper.ExecuteNonQuery("DELETE FROM MaintainStatus WHERE StatusId = @id", new SqlParameter("@id", id));
            return rows > 0;
        }
        #endregion
        #region 修改维修状态
        /// <summary>
        /// 修改维修状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(MaintainStatus model)
        {
            string sql = "UPDATE MaintainStatus SET StatusId=@StatusId,StatusDesc=@StatusDesc WHERE StatusId=@StatusId";
            int rows = SqlHelper.ExecuteNonQuery(sql
                , new SqlParameter("@StatusId", model.StatusId)
                , new SqlParameter("@StatusDesc", model.StatusDesc)
            );
            return rows > 0;
        }
        #endregion
        #region 根据维修状态Id查询
        /// <summary>
        /// 根据维修状态Id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MaintainStatus Get(int id)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM MaintainStatus WHERE StatusId=@StatusId", new SqlParameter("@StatusId", id));
            if (dt.Rows.Count > 1)
            {
                throw new Exception("more than 1 row was found");
            }
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            DataRow row = dt.Rows[0];
            MaintainStatus model = ToModel(row);
            return model;
        }
        #endregion
        #region 查询所有
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private static MaintainStatus ToModel(DataRow row)
        {
            MaintainStatus model = new MaintainStatus();
            model.StatusId = (int)row["StatusId"];
            model.StatusDesc = (string)row["StatusDesc"];
            return model;
        }

        public IEnumerable<MaintainStatus> ListAll()
        {
            List<MaintainStatus> list = new List<MaintainStatus>();
            DataTable dt = SqlHelper.ExecuteDataTable("SELECT * FROM MaintainStatus");
            foreach (DataRow row in dt.Rows)
            {
                list.Add(ToModel(row));
            }
            return list;
        }
        #endregion
    }
}
